package cn.com.blueInfo.bpm.template.service.impl;

import cn.com.blueInfo.bpm.template.convert.WorkItemConvert;
import cn.com.blueInfo.bpm.template.entity.WorkItem;
import cn.com.blueInfo.bpm.template.repository.WorkItemRepository;
import cn.com.blueInfo.bpm.template.request.WorkItemDTO;
import cn.com.blueInfo.bpm.template.response.WorkItemVO;
import cn.com.blueInfo.bpm.template.service.WorkItemService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 节点任务实例表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:30:44
 */
@Log4j2
@Service
public class WorkItemServiceImpl implements WorkItemService {

    @Autowired
    private WorkItemRepository workItemRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private WorkItemConvert workItemConvert;

    /**
     * 新增节点任务实例
     * @param workItemDTO 节点任务实例参数
     */
    @Override
    public ResultInfo<Integer> addWorkItem(WorkItemDTO workItemDTO) {
        WorkItem workItem = workItemConvert.dtoToEntity(workItemDTO);
        workItem.setId(snowflakeIdGenerator.nextId());
        Integer addSize = workItemRepository.createWorkItem(workItem);
        return ResultInfo.<Integer>builder().data(addSize).message("节点任务实例成功新增 {} 条").build();
    }

    /**
     * 批量新增节点任务实例
     * @param workItemDTOList 节点任务实例参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddWorkItem(List<WorkItemDTO> workItemDTOList) {
        List<WorkItem> workItemList = workItemConvert.dtoToEntity(workItemDTOList);
        for (WorkItem workItem : workItemList) workItem.setId(snowflakeIdGenerator.nextId());
        Integer addSize = workItemRepository.batchCreateWorkItem(workItemList);
        return ResultInfo.<Integer>builder().data(addSize).message("节点任务实例成功新增 {} 条").build();
    }

    /**
     * 删除节点任务实例
     * @param workItemId 节点任务实例ID
     */
    @Override
    public ResultInfo<Integer> removeWorkItem(Long workItemId) {
        Integer removeSize = workItemRepository.deleteWorkItem(workItemId);
        return ResultInfo.<Integer>builder().data(removeSize).message("节点任务实例成功删除 {} 条").build();
    }

    /**
     * 批量删除节点任务实例
     *
     * @param workItemIdList 节点任务实例ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveWorkItem(List<Long> workItemIdList) {
        return null;
    }

    /**
     * 更新部分节点任务实例
     * @param workItemUpdatePartialDTO 节点任务实例参数
     */
    @Override
    public ResultInfo<Integer> modifyWorkItemPartial(WorkItemDTO.UpdatePartialDTO workItemUpdatePartialDTO) {
        WorkItem workItem = workItemConvert.dtoToEntity(workItemUpdatePartialDTO);
        Integer modifySize = workItemRepository.updateWorkItemPartial(workItem);
        return ResultInfo.<Integer>builder().data(modifySize).message("节点任务实例成功更新 {} 条").build();
    }

    /**
     * 更新全部节点任务实例
     * @param workItemDTO 节点任务实例参数
     */
    @Override
    public ResultInfo<Integer> modifyWorkItem(WorkItemDTO workItemDTO) {
        WorkItem workItem = workItemConvert.dtoToEntity(workItemDTO);
        Integer modifySize = workItemRepository.updateWorkItem(workItem);
        return ResultInfo.<Integer>builder().data(modifySize).message("节点任务实例成功更新 {} 条").build();
    }

    /**
     * 查询一条节点任务实例
     * @param workItemId 节点任务实例ID
     */
    @Override
    public ResultInfo<WorkItemVO> queryWorkItemById(Long workItemId) {
        WorkItem workItem = workItemRepository.getWorkItemById(workItemId);
        WorkItemVO workItemVO = workItemConvert.entityToVO(workItem);
        return ResultInfo.<WorkItemVO>builder().data(workItemVO).message("节点任务实例查询成功").build();
    }

    /**
     * 分页查询节点任务实例
     * @param workItemQueryDTO 节点任务实例参数
     */
    @Override
    public ResultInfo<Page<WorkItemVO>> queryWorkItemPage(WorkItemDTO.QueryDTO workItemQueryDTO) {
        Page<WorkItem> workItemPage = workItemRepository.getWorkItemPage(workItemQueryDTO);
        Page<WorkItemVO> resultData = workItemConvert.convertPage(workItemPage);
        return ResultInfo.<Page<WorkItemVO>>builder()
                .data(resultData)
                .message("节点任务实例分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 节点任务实例 数量
     * @param workItemQueryDTO 节点任务实例参数
     */
    @Override
    public ResultInfo<Long> queryWorkItemCount(WorkItemDTO.QueryDTO workItemQueryDTO) {
        Long resultCount = workItemRepository.getWorkItemCount(workItemQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("节点任务实例数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 节点任务实例
     * @param workItemQueryDTO 节点任务实例参数
     */
    @Override
    public ResultInfo<List<WorkItemVO>> queryWorkItemList(WorkItemDTO.QueryDTO workItemQueryDTO) {
        List<WorkItem> workItemList = workItemRepository.getWorkItemList(workItemQueryDTO);
        List<WorkItemVO> resultData = workItemConvert.entityListToVoList(workItemList);
        return ResultInfo.<List<WorkItemVO>>builder()
                .data(resultData)
                .message("节点任务实例分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
